package com.bac.rentmap.web.controller.ad.user.view;

import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bac.rentmap.ad.flat.user.view.ViewStatistics;
import com.bac.rentmap.ad.flat.user.view.ViewStatisticsManager;

public class ViewStatisticsController extends AbstractController {
    private static final Logger log = Logger
            .getLogger(ViewStatisticsController.class.getName());

    private ViewStatisticsManager viewStatisticsManager;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Long flatAdId = Long.valueOf(request.getParameter("flatAdId"));
        List<ViewStatistics> viewStatistics = viewStatisticsManager
                .load(flatAdId);
        ModelAndView res = new ModelAndView("ad/manage/viewStatistics");
        if (viewStatistics != null) {
            log.info("Loaded " + viewStatistics.size() + " statistics entries");
            res.addObject("viewStatistics", viewStatistics);
        }
        return res;
    }

    @Required
    public void setViewStatisticsManager(
            ViewStatisticsManager viewStatisticsManager) {
        this.viewStatisticsManager = viewStatisticsManager;
    }
}
